package com.tianqu.tms.www.proxymember.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.proxymember.actionform.ProxymemberCreationInputActionForm;
import com.tianqu.tms.www.proxymember.controller.ProxyMemberCarUpdateResultController;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;

/**
 * @author zhangxueliang
 * 待办车辆修改结果action
 */
public class ProxyMemberCarUpdateResultAction extends TmsAction{
	private Logger logger = Logger
	.getLogger(ProxyMemberCarUpdateResultAction.class);
	
	private static final String BACK = "back";
	private static final String OFF = "off";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		if(!isTokenValid(request)){//如果两个值相等,即表单重复提交  
            this.saveToken(request);  
            setErrorsToRequest(request, TmsConstants.I0005 ,null);
            request.setAttribute(OFF, "");
            return mapping.findForward(TmsConstants.SUCCESS);
        }else{//当用户首次提交时返!isTokenValid()返回true  
        	 request.setAttribute(OFF, "信息添加完了");
            this.resetToken(request);//将用户session中的token清空  
        }
		ProxymemberCreationInputActionForm proxyForm = (ProxymemberCreationInputActionForm)form;
		if(proxyForm.getBackButton() != null){
			ProxyMemberCarSearchInputViewHelper viewhelp = 
				(ProxyMemberCarSearchInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			Codes code = Codes.getInstance();
			
			viewhelp.setCardType(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.PLATE_TYPE, viewhelp.getCardType())));
			
			viewhelp.setCarType(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_TYPE,viewhelp.getCarType())));
			viewhelp.setCarUse(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.USER_NATURE,viewhelp.getCarUse())));
			viewhelp.setColor(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COLOR,viewhelp.getColor())));
			viewhelp.setCountryStyle(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.VEHICLE_COUNTRY,viewhelp.getCountryStyle())));
			
			viewhelp.setGearbox(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.GEARBOX_TYPE,
					viewhelp.getGearbox())));
			viewhelp.setFuelType(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.FUEL_TYPE,viewhelp.getFuelType())));
			viewhelp.setOwnerType(String.valueOf(code.getItemCodeByItemIdAndItemValue(Codes.CAR_OWNER,viewhelp.getOwnerType())));
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			proxyForm.setBackButton(null);//给form null
			return mapping.findForward(BACK);
		}
		
		
		ProxyMemberCarUpdateResultController controller = new ProxyMemberCarUpdateResultController();
		controller.execute(uc);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
